package com.then.libary.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.util.Log;

import com.then.libary.bean.User;
import com.then.libary.dao.UserDao;

public class UserDaoImpl extends BaseDao implements UserDao {

	public UserDaoImpl(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
		// TODO Auto-generated constructor stub
	}

	@Override
	public List<User> getUsers() {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public boolean addLoginUser(String account, String password,int rember) {
		SQLiteDatabase sqLiteDatabase = getWritableDatabase();
		
		ContentValues values = new ContentValues();
		values.put("account", account);
		values.put("password", password);
		values.put("remember", rember);
		long count = sqLiteDatabase.insert(BaseDaoConfig.TABLE_NAME_1, "id",
				values);
		
		sqLiteDatabase.close();
		Log.e("message", count+"--");
		if (count == -1)
			return false;
		return true;
	}
	@Override
	public User findUserByAccount(String account) {
		User user = null;
		SQLiteDatabase sqLiteDatabase = getWritableDatabase();
		Cursor cursor = sqLiteDatabase.query(BaseDaoConfig.TABLE_NAME_1, null, "account=?", new String[]{account}, null, null, null);
		boolean flag = cursor.moveToFirst();
		Log.e("message_flag", flag+"");
		if(flag)
		{
			user = new User();
			String user_account = cursor.getString(cursor.getColumnIndex("account"));
			user.setAccount(user_account);
			int id = cursor.getInt(cursor.getColumnIndex("id"));
			user.setId(id);
			String password = cursor.getString(cursor.getColumnIndex("password"));
		}
		return user;
	}
	@Override
	public Map<String, User> getAllUserAccount() {
		Map<String, User> mapUser = new HashMap<String, User>();
		SQLiteDatabase sqLiteDatabase = getReadableDatabase();
		String[] select_colum = { "account", "password", "remember" };
		Cursor cursor = sqLiteDatabase.query(BaseDaoConfig.TABLE_NAME_1,
				select_colum, null, null, null, null, null);
		
		while (cursor.moveToNext()) {
			User user = new User();
			user.setAccount(cursor.getString(cursor.getColumnIndex("account")));
			user.setPassword(cursor.getString(cursor.getColumnIndex("password")));
			user.setRemember(cursor.getInt(cursor.getColumnIndex("remember")));
			mapUser.put(user.getAccount(), user);
		}
		
		sqLiteDatabase.close();
		return mapUser;
	}
	@Override
	public void updateUserCheck(String number, int rember) {
		// TODO Auto-generated method stub
		SQLiteDatabase sqLiteDatabase = getWritableDatabase();
		
		ContentValues values = new ContentValues();
		values.put("remember", rember);
		sqLiteDatabase.update(BaseDaoConfig.TABLE_NAME_1, values , "account=?", new String[]{number});
		sqLiteDatabase.close();
	}

}
